package com._119yn.host.gst200.database.dao.impl;

import com._119yn.host.gst200.database.dao.DeviceTypeDao;
import com._119yn.host.gst200.database.entity.DeviceType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceTypeDaoImpl implements DeviceTypeDao {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public long insert(DeviceType deviceType) {
        String sql = "insert into DEVICE_TYPE (DEVICE_TYPE_ID, DEVICE_TYPE_NAME, IMAGE_ID, PROCESS_TYPE, DEVICE_TYPE_NEW_NAME) values (?,?,?,?,?)";
        return jdbcTemplate.update(
                sql,
                deviceType.getDeviceTypeId(),
                deviceType.getDeviceTypeName(),
                deviceType.getImageId(),
                deviceType.getProcessType(),
                deviceType.getDeviceTypeNewName()
        );
    }

    @Override
    public long deleteByDeviceTypeId(Integer deviceTypeId) {
        String sql = "DELETE FROM DEVICE_TYPE WHERE DEVICE_TYPE_ID = ?";
        return jdbcTemplate.update(sql, deviceTypeId);
    }

    @Override
    public long updateByDeviceTypeId(DeviceType deviceType) {
        String sql = "UPDATE DEVICE_TYPE SET DEVICE_TYPE_NAME=?,IMAGE_ID=?,PROCESS_TYPE=?,DEVICE_TYPE_NEW_NAME=? WHERE DEVICE_TYPE_ID = ?";
        return jdbcTemplate.update(
                sql,
                deviceType.getDeviceTypeName(),
                deviceType.getImageId(),
                deviceType.getProcessType(),
                deviceType.getDeviceTypeNewName(),
                deviceType.getDeviceTypeId()
        );
    }

    @Override
    public DeviceType selectByDeviceTypeId(Integer deviceTypeId) {
        String sql = "SELECT * FROM DEVICE_TYPE WHERE DEVICE_TYPE_ID = ?";
        List<DeviceType> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DeviceType.class), deviceTypeId);
        if (list.isEmpty()) {
            return null;
        }
        return list.getFirst();
    }

    @Override
    public List<DeviceType> selectAll() {
        String sql = "SELECT * FROM DEVICE_TYPE";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DeviceType.class));
    }

}
